package com.lvtongka.app.ui.security

import android.text.TextUtils
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.lvtongka.app.base.BaseActivity
import com.lvtongka.app.common.CommonUtils
import com.lvtongka.app.databinding.ActivitySecurityBinding
import com.lvtongka.app.dialog.DialogRealName
import com.lvtongka.app.dialog.DialogSureCancel
import com.lvtongka.app.fragment.common.DialogHelper
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.common.WebConstants

class SecurityActivity : BaseActivity<ActivitySecurityBinding, SecurityViewModel>() {
    private var dialogRealName: DialogRealName? = null

    override fun initViewModel(): SecurityViewModel {
        return ViewModelProvider(this)[SecurityViewModel::class.java]
    }

    override fun initEvent() {
        binding.header.imgBack.setOnClickListener { finish() }
        viewModel.error.observe(this) {
            hideLoading()
            showToast(it)
        }

        binding.tvGoAuth.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            val isRealName = SPHelper.getBoolean(SPKConstants.IS_REAL_NAME)
            if (!isRealName) {
                DialogHelper.showRealNameHintDialog(this){
                    showRealNameDialog()
                }
            }
        }

        binding.llLogOff.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            showLogOffDialog()
        }

        binding.llProtocolManage.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            jumpWeb(WebConstants.PRIVACY_LIST, true)
        }

        viewModel.result.observe(this) {
            hideLoading()
            SPHelper.clearAll()
            showToast("注销成功")
            setResult(RESULT_OK)
            finish()
        }

        viewModel.realName.observe(this) {
            hideLoading()
            dialogRealName?.dismiss()
            showToast("实名认证成功")
            binding.tvSm.text = "已实名"
            binding.tvGoAuth.visibility = View.GONE
        }
    }

    override fun initData() {
        binding.header.tvTitle.text = "账户安全"
        binding.header.imgBack.visibility = View.VISIBLE
        binding.header.statusBarView.layoutParams.height = CommonUtils.getStatusBarHeight()

        val isRealName = SPHelper.getBoolean(SPKConstants.IS_REAL_NAME)
        if (!isRealName) {
            binding.tvSm.text = "未实名"
            binding.tvGoAuth.visibility = View.VISIBLE
        } else {
            binding.tvSm.text = "已实名"
            binding.tvGoAuth.visibility = View.GONE
        }
    }

    override fun initBinding(): ActivitySecurityBinding {
        return ActivitySecurityBinding.inflate(layoutInflater)
    }

    private fun showLogOffDialog() {
        val sureCancel = DialogSureCancel(this)
        sureCancel.setTitle("注销")
        sureCancel.setContent("确定要注销吗？")
        sureCancel.setCancelable(false)
        sureCancel.setCanceledOnTouchOutside(false)
        sureCancel.setSureListener {
            showLoading("注销中...")
            viewModel.getLogOff()
            sureCancel.cancel()
        }
        sureCancel.setCancelListener {
            sureCancel.cancel()
        }
        sureCancel.show()
    }

    private fun showRealNameDialog() {
        dialogRealName = DialogRealName(this)
//        dialogRealName?.setCancelable(false)
//        dialogRealName?.setCanceledOnTouchOutside(false)
        dialogRealName?.setSureListener {
            // 实名认证
            val realName = dialogRealName?.getRealName()
            val realId = dialogRealName?.getRealId()
            if (TextUtils.isEmpty(realName)) {
                showToast("请输入真实姓名")
                return@setSureListener
            }
            if (TextUtils.isEmpty(realId)) {
                showToast("请输入身份证号")
                return@setSureListener
            }
            showLoading("实名认证中...")
            viewModel.realNameAuthentication(realName!!, realId!!)
        }
        dialogRealName?.show()
    }
}